﻿using System;
using System.Text;
using System.Windows.Forms;
using ReportWizard.DataAccess;
using ReportWizard.Forms;
using ReportWizard.Helpers;
using ReportWizard.Interface;
using ReportWizard.Properties;
using System.Collections.Generic;

namespace ReportWizard.Controls
{
    public partial class TemplateTextSelect : UserControl
    {
        #region properties

        public IBookmark CurrentBookmark { get; set; }

        public bool IsSelected { get; set; }
        
        private string OldValue { get; set; }

        public string FieldTitle
        {
            get { return lFieldName.Text.Trim(); }
            set { lFieldName.Text = value; }
        }

        public string FieldValue
        {
            get { return tbFieldValue.Text.Trim(); }
            set { tbFieldValue.Text = value; }
        }

        #endregion

        public TemplateTextSelect(IBookmark wb)
        {
            CurrentBookmark = wb;

            InitializeComponent();

            FillBookmarkInfo();
        }

        public void Clear()
        {
            tbFieldValue.Text = string.Empty;
        }

        private void FillBookmarkInfo()
        {
            FieldTitle = CurrentBookmark.Name.Replace('_', ' ');
            Tag = CurrentBookmark;

            if (CurrentBookmark is WordBookmark)
            {
                if (!string.IsNullOrEmpty(CurrentBookmark.Description))
                {
                    pbInfo.Image = Resources.IconInfo;
                    pbInfo.Visible = true;
                    ttBookmark.SetToolTip(pbInfo, CurrentBookmark.Description);
                }
            }
            else if (CurrentBookmark is BookmarkGroup)
            {
                List<WordBookmark> bList = WordBookmark.SelectInGroup(CurrentBookmark.Id);
                if (bList != null)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("• {0} •{1}", CurrentBookmark.Name, Environment.NewLine);
                    sb.AppendLine();
                    for (int i = 0; i < bList.Count; i++)
                    {
                        sb.AppendFormat("{0} - {1}{2}", i + 1, bList[i].Name.Replace('_',' '), Environment.NewLine);
                    }

                    pbInfo.Image = Resources.IconBookOpen;
                    pbInfo.Visible = true;
                    ttBookmark.SetToolTip(pbInfo, sb.ToString());
                }
            }
        }

        private void bSelectFieldValue_Click(object sender, EventArgs e)
        {
            Globals.TargetFormProfile = CurrentBookmark.Name;
            SelectForm sf = new SelectForm(CurrentBookmark);
            if (sf.ShowDialog(Parent) == DialogResult.OK && !string.IsNullOrEmpty(sf.TargetText))
            {
                IsSelected = true;
                tbFieldValue.Text = sf.TargetText;
            }
        }

        private void tbFieldValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (IsSelected)
            {
                IsSelected = false;
                OldValue = tbFieldValue.Text.Trim();
            }
        }

        private void tbFieldValue_TextChanged(object sender, EventArgs e)
        {
            if (tbFieldValue.Text.Trim().Equals(OldValue))
            {
                IsSelected = true;
            }
        }
    }
}
